/**
 * Copyright 2007-2011 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.spring.annotation;

import org.jadapter.Adapter;
import org.jadapter.registry.AdapterRegistry;
import org.jadapter.spring.SpringBeanAdapter;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Set;

/**
 * @author pjesi
 * @since 0.4
 */
public class AdapterAnnotationBeanDefinitionPostProcessor
        implements
        SmartInstantiationAwareBeanPostProcessor,
        BeanFactoryAware {


    private AdapterRegistry registry;
    private BeanFactory beanFactory;

    private Set<String> registeredBeans = new HashSet<String>();

    @Autowired
    public AdapterAnnotationBeanDefinitionPostProcessor(AdapterRegistry registry) {
        this.registry = registry;
    }

    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    public Class predictBeanType(Class beanClass, String beanName) {
        if (beanClass.isAnnotationPresent(org.jadapter.annotations.Adapter.class)
                && !registeredBeans.contains(beanName)) {
            Adapter adapter = new SpringBeanAdapter(beanClass, beanName, beanFactory);
            registry.register(adapter);
            registeredBeans.add(beanName);
        }

        return null;
    }

    public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) {
        return null;
    }

    public Object getEarlyBeanReference(Object bean, String beanName) {
        return bean;
    }


    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }

    public Object postProcessBeforeInstantiation(Class beanClass, String beanName){
        return null;
    }

    public boolean postProcessAfterInstantiation(Object bean, String beanName) {
        return true;
    }

    public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                                                    PropertyDescriptor[] pds, Object bean,
                                                    String beanName) {
        return pvs;
    }

}
